import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yysk_app/model/order_model/user_box_transfer_detail_model.dart';
import 'package:yysk_app/model/order_model/user_collection_transfer_detail_model.dart';
import 'package:yysk_app/model/order_model/user_prop_transfer_detail_model.dart';
import 'package:yysk_app/net/api/order_api.dart';
import 'package:yysk_app/net/http.dart';
import 'package:yysk_app/utils/colors.dart';
import 'package:yysk_app/utils/helper.dart';
import 'package:yysk_app/widget/card_list_tile_widget.dart';
import 'package:yysk_app/widget/custom_app_bar2.dart';
import 'package:yysk_app/widget/image_asset_widget.dart';
import 'package:yysk_app/widget/item_card_widget.dart';
import 'package:yysk_app/widget/lazy_network_image.dart';

class PropTransferOutPage extends StatefulWidget {
  static const path = '/propTransferOutPage';
  const PropTransferOutPage({super.key});
  @override
  State<StatefulWidget> createState() => _PropTransferOutPage();
}

class _PropTransferOutPage extends State<PropTransferOutPage> {
  late String type = '1'; //1藏品2盲盒3道具
  late String id = '2'; //1转出2接收
  late String transferType = '1'; //1转出2接收
  UserCollectionTransferDetailModel? listCollection;
  UserBoxTransferDetailModel? listBox;
  UserPropTransferDetailModel? listProp;
  @override
  void initState() {
    super.initState();
    type = Get.parameters['type'] ?? Get.arguments['type'];
    id = Get.parameters['id'] ?? Get.arguments['id'];
    if (type == '1') {
      getCollectionOrderDetail();
    } else if (type == '2') {
      getBoxOrderDetail();
    } else {
      getPropOrderDetail();
    }
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    double top = MediaQuery.of(context).padding.top;

    return Scaffold(
      body: Column(
        children: [
          Expanded(
            child: Stack(
              children: [
                SizedBox(
                  width: context.width,
                  child: ImageAssetWidget(
                    width: 375.px,
                    height: 196.px,
                    alignment: Alignment.topCenter,
                    imageUrl: 'static/images/prop_transfer_bg.png',
                  ),
                ),

                SingleChildScrollView(
                  scrollDirection: Axis.vertical,
                  padding: EdgeInsets.fromLTRB(12, 21, 12, 0),
                  child: Column(
                    children: [
                      SizedBox(height: top),
                      CustomAppBar2(
                        title: '${transferType == '1' ? '转出' : '接收'}详情',
                        context: context,
                      ),
                      hiSpace(height: 20.px),
                      Container(
                        padding: EdgeInsets.fromLTRB(15, 18, 15, 28),
                        decoration: BoxDecoration(
                          color: AppColors.backgroundColor,
                          borderRadius: BorderRadius.circular(8.px),
                        ),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            _buildOrderCollectionInfo(item: 1),
                            hiSpace(height: 26.px),
                            Divider(color: Color(0xFFEEEEEE), thickness: 1),
                            hiSpace(height: 12.px),
                            Text(
                              '订单信息',
                              style: TextStyle(
                                fontSize: 16.px,
                                fontWeight: FontWeight.normal,
                              ),
                            ),
                            _buildOrderDetail(),
                            hiSpace(height: 35.px),
                            type == '1'
                                ? Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    hiSpace(height: 35.px),
                                    Text(
                                      '藏品编号',
                                      style: TextStyle(
                                        fontSize: 16.px,
                                        fontWeight: FontWeight.normal,
                                      ),
                                    ),
                                    hiSpace(height: 16.px),

                                    listCollection != null
                                        ? (listCollection!
                                                .tokenIdList!
                                                .isNotEmpty
                                            ? Wrap(
                                              children: [
                                                ...listCollection!.tokenIdList!
                                                    .map(
                                                      (item) =>
                                                          _buildOrderCode(item),
                                                    ),
                                              ],
                                            )
                                            : Center(
                                              child: Column(
                                                children: [
                                                  Image(
                                                    width: 174.px,
                                                    height: 133.px,
                                                    image: AssetImage(
                                                      'static/images/no_data.png',
                                                    ),
                                                    fit: BoxFit.cover,
                                                  ),
                                                  SizedBox(height: 20.px),
                                                  Text(
                                                    '藏品正在上链中',
                                                    style: TextStyle(
                                                      color:
                                                          AppColors.fontColorA,
                                                      fontSize: 12.px,
                                                    ),
                                                  ),
                                                ],
                                              ),
                                            ))
                                        : Text(''),
                                  ],
                                )
                                : SizedBox(),
                            hiSpace(height: 16.px),
                            // listDetail.map((String msg) => _buildOrderCode()),
                            // type=='1'?_buildOrderCode():SizedBox(),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  _buildOrderCode(item) {
    return
    // [{}, {}, {}].map(
    //   (e) =>
    Stack(
      children: [
        ImageAssetWidget(
          imageUrl: 'static/images/code_ico.png',
          width: 18.px,
          height: 18.px,
        ),
        Container(
          height: 18.px,
          margin: EdgeInsets.only(left: 9.px),
          padding: EdgeInsets.fromLTRB(9.px, 0, 4.px, 0),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.only(
              topRight: Radius.circular(8.px),
              bottomRight: Radius.circular(8.px),
            ),
            color: Colors.black.withOpacity(0.05),
          ),
          child: Text(
            '编号 $item',
            style: TextStyle(fontSize: 12.px, color: const Color(0xFF3D3D3D)),
          ),
        ),
      ],
    );
  }

  _buildStatusType(int type) {
    switch (type) {
      case 1:
        return '待支付';
      case 2:
        return '已完成';
      case 3:
        return '已取消';
      default:
        return '--';
    }
  }

  Widget _buildOrderDetail() {
    return ItemCardWidget(
      child: Column(
        children: [
          CardListTileWidget(
            label: '数量',
            value:
                '*${type == '1' ? listCollection?.count : (type == '2' ? listBox?.count : listProp?.count)}',
          ),
          // CardListTileWidget(label: '服务费', value: '￥88.88'),
          // CardListTileWidget(label: '支付方式', value: '连连钱包'),
          // CardListTileWidget(label: '订单状态', value: _buildStatusType(1)),
          CardListTileWidget(
            label: '发送方',
            value:
                '${type == '1' ? listCollection?.sendAddress : (type == '2' ? listBox?.sendUserAccount : listProp?.sendUserAccount)}',
          ),
          CardListTileWidget(
            label: '接收方',
            value:
                '${type == '1' ? listCollection?.receiveAddress : (type == '2' ? listBox?.receiveUserAccount : listProp?.receiveUserAccount)}',
          ),
          CardListTileWidget(
            label: '转赠时间',
            value:
                '${type == '1' ? listCollection?.createTime : (type == '2' ? listBox?.createTime : listProp?.createTime)}',
          ),
          CardListTileWidget(
            label: '订单编号',
            value:
                '${type == '1' ? listCollection?.orderCode : (type == '2' ? listBox?.orderCode : listProp?.orderCode)}',
            canCopy: true,
          ),
        ],
      ),
    );
  }

  // 藏品信息
  Widget _buildOrderCollectionInfo({required item}) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Row(
          children: [
            LazyImage(
              imageUrl:
                  '${type == '1' ? listCollection?.coverImageUrl : (type == '2' ? listBox?.boxCoverImageUrl : listProp?.propImageUrl)}',
              width: 68.px,
              height: 68.px,
              borderRadius: 4.px,
            ),
            hiSpace(width: 12.px),
            SizedBox(
              height: 68.px,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  SizedBox(
                    width: 240.px,
                    child: Text(
                      '${type == '1' ? listCollection?.collectionName : (type == '2' ? listBox?.title : listProp?.title)}',
                      style: TextStyle(
                        fontSize: 14.px,
                        color: AppColors.backgroundColorBlack,
                        fontWeight: FontWeight.normal,
                      ),
                      overflow: TextOverflow.ellipsis,
                    ),
                  ),
                  type == '1'
                      ? SizedBox(
                        width: 240.px,
                        child: Text(
                          type == '1'
                              ? '${listCollection?.daoName}'
                              : (type == '2' ? '' : ''),
                          style: TextStyle(
                            fontSize: 10.px,
                            color: AppColors.backgroundColorBlack,
                          ),
                          overflow: TextOverflow.ellipsis,
                        ),
                      )
                      : SizedBox(),
                ],
              ),
            ),
          ],
        ),
      ],
    );
  }

  //藏品流转
  Future<void> getCollectionOrderDetail() async {
    try {
      await http.netFetch<UserCollectionTransferDetailModel>(
        '${OrderUrl.userCollectionTransferDetail}/$id',
        method: Method.get,
        success: (result) {
          setState(() {
            listCollection = result.data;
          });
        },
      );
    } catch (e) {
      debugPrint(e.toString());
    }
  }

  //盲盒流转
  Future<void> getBoxOrderDetail() async {
    try {
      await http.netFetch<UserBoxTransferDetailModel>(
        '${OrderUrl.userBoxTransferDetail}/$id',
        method: Method.get,
        success: (result) {
          setState(() {
            listBox = result.data;
          });
        },
      );
    } catch (e) {
      debugPrint(e.toString());
    }
  }

  //道具流转
  Future<void> getPropOrderDetail() async {
    try {
      await http.netFetch<UserPropTransferDetailModel>(
        '${OrderUrl.userPropTransferRecord}/$id',
        method: Method.get,
        success: (result) {
          setState(() {
            listProp = result.data;
          });
        },
      );
    } catch (e) {
      debugPrint(e.toString());
    }
  }
}
